package com.andy.customerService.service.impl;


import com.alibaba.druid.support.json.JSONUtils;
import com.andy.customerService.entity.User;
import com.andy.customerService.enums.ExceptionEnum;
import com.andy.customerService.exception.BusinessException;
import com.andy.customerService.mapper.UserMapper;
import com.andy.customerService.pojo.request.UserRequestParam;
import com.andy.customerService.service.UserService;
import com.andy.customerService.vo.JsonBaseResult;
import com.andy.customerService.vo.JsonPageResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.json.Json;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Andy
 * @since 2021-12-24
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    /**
     * 添加用户
     *
     * @param user
     */
    @Transactional
    @CacheEvict(value = "users",allEntries = true)
    @Override
    public void addUser(User user) {
        log.info("系统管理-添加用户，参数：{}", user);
        if (StringUtils.isBlank(user.getPassword())) {
            throw new BusinessException(ExceptionEnum.PASSWORD_CANNOT_BE_NULL);
        }
        userMapper.insert(user);
    }

    /**
     * 根据id查询用户
     */
    @Override
    public JsonBaseResult<User> queryUserById(Long id) {
        log.info("用户管理-根据id查询用户 入参：{}", id);
        JsonBaseResult<User> result = new JsonBaseResult<>();
        User user = userMapper.selectById(id);
//        User user1 = userMapper.selectByPramaryKey(id);

        result.setResult(user);
        return result;
    }

    /**
     * 根据id修改用户
     */
    @CacheEvict(value = "users",allEntries = true)
    @Transactional
    @Override
    public void editUserById(User user) {
        log.info("用户管理-根据id修改用户 入参：{}", user);
        if (null == user.getId()) {
            throw new BusinessException(ExceptionEnum.USER_ID_CAN_NOT_BE_NULL);
        }
        userMapper.updateById(user);
    }

    /**
     * 根据id删除用户
     *
     * @param id
     */
    @CacheEvict(value = "users",allEntries = true)
    @Transactional
    @Override
    public void deleteUserById(Long id) {
        log.info("用户管理-根据id删除用户 入参：{}", id);
        userMapper.deleteById(id);
    }

    /**
     * 根据条件查询用户列表
     *
     * @param requestParam
     */
//    @Cacheable(value = "userList:cache",
//            key = "#root.methodName+'_'+" +
//                    "#requestParam.startTime+'_'+" +
//                    "#requestParam.endTime+'_'+" +
//                    "#requestParam.name+'_'+" +
//                    "#requestParam.pageNo+'_'+" +
//                    "#requestParam.pageSize+'_'+" +
//                    "#requestParam.orgId+'_'")
    @Cacheable(cacheNames = "users")
    @Override
    public JsonPageResult<Object> queryUserListByParam(UserRequestParam requestParam) {

//        List<User> users =  redisTemplate.opsForList().range("users",0,-1);

        log.info("用户管理-根据条件查询用户列表");
        //配置分页
        Page<User> page = PageHelper.startPage(requestParam.getPageNo(), requestParam.getPageSize());

        //根据条件查询用户列表
        List<User> userList = userMapper.queryUserListByParam(requestParam);

        //封装返回数据
        JsonPageResult<Object> pageResult = new JsonPageResult<>();

        pageResult.setResult(userList);
        int totalNum = (int) page.getTotal();
        pageResult.setTotalNum(totalNum);
        pageResult.setPageNo(requestParam.getPageNo());
        pageResult.setPageSize(requestParam.getPageSize());
//        redisTemplate.opsForValue().set("users", JSONUtils.toJSONString(userList),10, TimeUnit.SECONDS);
//        redisTemplate.opsForList().rightPush("users",userList);
        return pageResult;
    }


}
